package org.sxp.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.sxp.common.annotation.SysLog;
import org.sxp.common.exception.AppException;
import org.sxp.common.exception.ParamException;
import org.sxp.common.utils.Constant;
import org.sxp.common.utils.R;
import org.sxp.common.web.WrapperResponse;
import org.sxp.modules.sys.entity.SysMenuEntity;
import org.sxp.modules.sys.service.SysMenuService;

import java.security.Principal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 系统菜单
 *
 *
 */
@RestController
@RequestMapping("/sys/menu")
@Api(description="Menu", tags = "菜单管理")
@SysLog(modules = "sys", value = "菜单管理")
public class SysMenuController extends AbstractController {
	@Autowired
	private SysMenuService sysMenuService;

	/**
	 * 导航菜单
	 */
	@RequestMapping("/nav")
	public WrapperResponse<List<SysMenuEntity>> nav(){
		List<SysMenuEntity> menuList = sysMenuService.getUserMenuTree(getUserId());
		return WrapperResponse.success(menuList);
	}

	/**
	 * 所有菜单列表
	 */
	@RequestMapping("/list")
	public List<SysMenuEntity> list(Long parentId){
		List<SysMenuEntity> menuList = sysMenuService.list(
				new LambdaQueryWrapper<SysMenuEntity>()
						.eq(parentId != null,SysMenuEntity::getParentId, parentId).orderByAsc(SysMenuEntity::getOrderNum)
		);
		// 优化
		Map<Long, String> map = menuList.stream().collect(Collectors.toMap(SysMenuEntity::getMenuId, SysMenuEntity::getName));
		for(SysMenuEntity sysMenuEntity : menuList){
			if(map.containsKey(sysMenuEntity.getParentId())){
				sysMenuEntity.setParentName(map.get(sysMenuEntity.getParentId()));
			}
		}

		return menuList;
	}

	/**
	 * 所有菜单列表
	 */
	@RequestMapping("/userList")
	public List<SysMenuEntity> userList(){
		List<SysMenuEntity> menuList = sysMenuService.getUserMenuList(getUserId());
		// 优化
		Map<Long, String> map = menuList.stream().collect(Collectors.toMap(SysMenuEntity::getMenuId, SysMenuEntity::getName));
		for(SysMenuEntity sysMenuEntity : menuList){
			if(map.containsKey(sysMenuEntity.getParentId())){
				sysMenuEntity.setParentName(map.get(sysMenuEntity.getParentId()));
			}
		}

		return menuList;
	}
	
	/**
	 * 选择菜单(添加、修改菜单)
	 */
	@RequestMapping("/select")
	@RequiresPermissions("sys:menu:select")
	public WrapperResponse<List<SysMenuEntity>> select(){
		//查询列表数据
		List<SysMenuEntity> menuList = sysMenuService.queryNotButtonList();
		
		//添加顶级菜单
		SysMenuEntity root = new SysMenuEntity();
		root.setMenuId(0L);
		root.setName("一级菜单");
		root.setParentId(-1L);
		root.setOpen(true);
		menuList.add(root);
		
		return WrapperResponse.success(menuList);
	}
	
	/**
	 * 菜单信息
	 */
	@RequestMapping("/info/{menuId}")
	@RequiresPermissions("sys:menu:info")
	public WrapperResponse<SysMenuEntity> info(@PathVariable("menuId") Long menuId){
		SysMenuEntity menu = sysMenuService.getById(menuId);
		return WrapperResponse.success(menu);
	}
	
	/**
	 * 保存
	 */
	@SysLog("保存菜单")
	@RequestMapping("/save")
	@RequiresPermissions("sys:menu:save")
	public WrapperResponse save(@RequestBody SysMenuEntity menu){
		//数据校验
		verifyForm(menu);
		
		sysMenuService.save(menu);
		
		return WrapperResponse.success(null);
	}
	
	/**
	 * 修改
	 */
	@SysLog("修改菜单")
	@RequestMapping("/update")
	@RequiresPermissions("sys:menu:update")
	public WrapperResponse update(@RequestBody SysMenuEntity menu){
		//数据校验
		verifyForm(menu);
				
		sysMenuService.updateById(menu);
		
		return WrapperResponse.success(null);
	}
	
	/**
	 * 删除
	 */
	@SysLog("删除菜单")
	@RequestMapping("/delete")
	@RequiresPermissions("sys:menu:delete")
	public WrapperResponse delete(long menuId){
		if(menuId <= 31){
			throw new AppException("系统菜单，不能删除");
		}

		//判断是否有子菜单或按钮
		List<SysMenuEntity> menuList = sysMenuService.queryListParentId(menuId, null);
		if(menuList.size() > 0){
			throw new AppException("请先删除子菜单或按钮");
		}

		sysMenuService.delete(menuId);

		return WrapperResponse.success(null);
	}
	
	/**
	 * 验证参数是否正确
	 */
	private void verifyForm(SysMenuEntity menu){
		if(StringUtils.isBlank(menu.getName())){
			throw new ParamException("菜单名称不能为空");
		}
		
		if(menu.getParentId() == null){
			throw new ParamException("上级菜单不能为空");
		}
		
		//菜单
		if(menu.getType() == Constant.MenuType.MENU.getValue()){
			if(StringUtils.isBlank(menu.getUrl())){
				throw new ParamException("菜单URL不能为空");
			}
		}
		
		//上级菜单类型
		int parentType = Constant.MenuType.CATALOG.getValue();
		if(menu.getParentId() != 0){
			SysMenuEntity parentMenu = sysMenuService.getById(menu.getParentId());
			parentType = parentMenu.getType();
		}
		
		//目录、菜单
		if(menu.getType() == Constant.MenuType.CATALOG.getValue() ||
				menu.getType() == Constant.MenuType.MENU.getValue()){
			if(parentType != Constant.MenuType.CATALOG.getValue()){
				throw new ParamException("上级菜单只能为目录类型");
			}
			return ;
		}
		
		//按钮
		if(menu.getType() == Constant.MenuType.BUTTON.getValue()){
			if(parentType != Constant.MenuType.MENU.getValue()){
				throw new ParamException("上级菜单只能为菜单类型");
			}
			return ;
		}
	}
}
